<template>
    <!-- 出库退货审核弹窗 -->
    <div class="dialog-box">
        <el-dialog v-model="open" title="出库退货审核" width="1000" :close-on-click-modal="false">
            <!-- 订单信息 -->
            <div class="order-info">
                <div class="info-list">
                    <div class="info-item">
                        <span>出库退货单号</span>
                        <span>{{ form.return_order_no }}</span>
                    </div>
                    <div class="info-item">
                        <span>关联补库单号</span>
                        <span>{{ form.outbound_order_no }}</span>
                    </div>
                    <div class="info-item">
                        <span>退货金额</span>
                        <span>{{ form.return_amount }}</span>
                    </div>
                    <div class="info-item">
                        <span>退货日期</span>
                        <span>{{ form.return_date }}</span>
                    </div>
                    <div class="info-item">
                        <span>客户</span>
                        <span>{{ form.customer_name }}</span>
                    </div>
                    <div class="info-item">
                        <span>提交人</span>
                        <span>{{ form.creator_name }}</span>
                    </div>

                </div>
            </div>

            <!-- 出库产品 -->
            <div class="product-section">
                <div class="section-title">出库产品</div>
                <div class="table-container">
                    <el-table :data="productList" border style="width: 100%">
                        <el-table-column prop="product_name" label="产品名称" align="center" />
                        <el-table-column prop="specification" label="规格型号" align="center" />
                        <el-table-column prop="open_unit_name" label="拆零单位" align="center" />
                        <el-table-column prop="return_num" label="退货数量" align="center" />
                        <el-table-column prop="price" label="退货单价" align="center" />
                    </el-table>
                </div>

            </div>


            <!-- 审核意见 -->
            <div class="audit-section">
                <div class="section-title">审核意见</div>
                <el-form :model="form" label-width="0">
                    <el-form-item>
                        <el-input v-model="form.audit_note" type="textarea" placeholder="请输入你的备注" :rows="4" />
                    </el-form-item>
                </el-form>
            </div>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="open = false">取消</el-button>
                    <el-button @click="handleReject" plain>审核拒绝</el-button>
                    <el-button type="primary" color="#626aef" @click="handleApprove">审核通过</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import api from "@/api";
import { ElMessage } from "element-plus";
const open = ref<boolean>(false);
const form = ref({
    id: "",
    return_order_no: "",
    outbound_order_no: "",
    return_amount: "",
    return_date: "",
    customer_name: "",
    creator_name: "",
    created_at: "",
    audit_note: ""
});
const emit = defineEmits(['refresh']);

// 模拟产品数据
const productList = ref([
]);

// 根据id获取退货产品数据
const getProductList = async () => {
    const res = await api.Outbound.getoutboundReturnProductListByOrderId({ id: form.value.id });
    if (res.code == 200) {
        productList.value = res.data.product_data;
    }
}

// 打开弹窗
const openLoading = (item: any) => {
    console.log(item);
    if (item) {
        form.value = { ...item };
        open.value = true;
    }
    getProductList();
};
// 审核通过
const handleApprove = async () => {
    const res = await api.Outbound.auditReturn({ id: form.value.id, audit_note: form.value.audit_note, status: 2 });
    if (res.code == 200) {
        ElMessage.success('审核通过');
        open.value = false;
        emit('refresh');
    } else {
        ElMessage.error(res.message || '审核失败');
    }
};
// 审核拒绝
const handleReject = async() => {
    const res = await api.Outbound.auditReturn({ id: form.value.id, audit_note: form.value.audit_note, status: 3 });
    if (res.code == 200) {
        ElMessage.success('审核不通过');
        open.value = false;
        emit('refresh');
    } else {
        ElMessage.error(res.message || '审核失败');
    }
};

defineExpose({
    openLoading,
});
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
    --el-dialog-padding-primary: 0px;
}

::v-deep .el-dialog__header {
    margin: 0;
    padding: 14px 20px;
    border-bottom: 1px solid #f0f0f0;

    .el-dialog__title {
        font-size: 16px;
        font-weight: 500;
    }
}

::v-deep .el-dialog__body {
    padding: 20px;
}

::v-deep .el-dialog__footer {
    padding: 10px 20px;
    border-top: 1px solid #f0f0f0;
}

.order-info {
    background: #f8f9fa;
    border-radius: 2px;
    padding: 16px 20px;
    margin-bottom: 24px;

    .info-list {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 16px;

        .info-item {
            display: flex;
            align-items: center;

            span:first-child {
                color: #909399;
                margin-right: 8px;
                white-space: nowrap;
            }

            span:last-child {
                color: #303133;
            }
        }
    }
}

.section-title {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    margin-bottom: 16px;
    display: flex;
    align-items: center;

    &::before {
        content: '';
        width: 2px;
        height: 14px;
        background: #626aef;
        margin-right: 8px;
        border-radius: 1px;
    }
}

.product-section {
    margin-bottom: 24px;

    .table-container {
        margin-bottom: 16px;
    }

    .table-summary {
        display: flex;
        justify-content: space-between;
        gap: 32px;
        padding: 8px 16px;
        background: #f8f9fa;
        border-radius: 2px;

        .summary-item {
            display: flex;
            align-items: center;
            gap: 15px;
        }
    }
}

.attachment-section {
    margin-bottom: 24px;

    .attachment-list {
        display: flex;
        flex-direction: column;
        gap: 12px;

        .attachment-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 16px;
            background: #f8f9fa;
            border-radius: 2px;

            .file-info {
                display: flex;
                align-items: center;
                gap: 8px;

                .file-icon {
                    width: 24px;
                    height: 24px;
                }

                .file-name {
                    color: #303133;
                }

                .file-size {
                    color: #909399;
                    font-size: 12px;
                }
            }

            .file-actions {
                display: flex;
                align-items: center;
                gap: 8px;

                .action-btn {
                    color: #626aef;
                    cursor: pointer;

                    &:hover {
                        color: #4318ff;
                    }
                }

                .action-divider {
                    color: #dcdfe6;
                }
            }
        }
    }
}

.audit-section {
    .el-textarea {
        --el-input-bg-color: #f8f9fa;
    }
}

:deep(.el-table) {
    --el-table-header-bg-color: #f8f9fa;
    --el-table-border-color: #ebeef5;
    --el-table-header-text-color: #606266;
}

:deep(.el-button--primary) {
    --el-button-hover-bg-color: #4318ff;
    --el-button-hover-border-color: #4318ff;
}
</style>
